package cn.wolfcode.trip.app.controller;


import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.TravelCommendQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 游记资源控制器
 */
@RestController
@RequestMapping("travels")
public class TravelController {
    @Autowired
    private ITravelService travelService;
    @Autowired
    private ITravelCommendService travelCommendService;

    /**
     * 新增游记接口
     * @param travel
     * @return
     */
    @PostMapping
    public JSONResult save(Travel travel){
        travelService.saveOrUpdate(travel);
        return new JSONResult();
    }

    /**
     * 编辑游记接口
     * @param travel
     * @return
     */
    @PutMapping("/{id}")
    public JSONResult update(Travel travel){
        travelService.saveOrUpdate(travel);
        return new JSONResult();
    }

    /**
     * 获取游记接口
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Travel getTravelById(@PathVariable Long id){
        return travelService.getTravelById(id);
    }

    /**
     * 获取所有游记分页接口
     * @param qo
     * @return
     */
    @GetMapping()
    public PageInfo getTravelQuery(TravelQueryObject qo){
        qo.setState(Travel.STATUS_RELEASE);//设置状态为已发布
        PageInfo pageInfo = travelService.query(qo);
        return pageInfo;
    }

    /**
     * 获取每周/每月推荐游记接口
     * @param qo
     * @return
     */
    @GetMapping("/commends")
    public PageInfo getTravelCommendQuery(TravelCommendQueryObject qo){
        qo.setOrderBy("schedule desc");//按照推荐时间倒叙排列
        qo.setIsCommend(true);//设置为查询App需要的数据
        PageInfo pageInfo = travelCommendService.query(qo);
        return pageInfo;
    }
}
